[[ package gen;

import java.util.*;

import se.rupy.http.*;
import se.rupy.memory.*;
import se.rupy.sprout.*;
import se.rupy.content.*;

public class feed extends Sprout {
	public String path() { return "/feed"; }
	public void filter(Event event) throws Event, Exception {
		event.query().parse();
		event.reply().type("text/xml");
		Output out = event.output();
		Object key = event.session().get("key");
		LinkedList list = Article.get("article", 0, 5);
		Iterator it = list.iterator(); ]]

<?xml version="1.0" encoding="utf-8"?>

[[		if(event.bit("atom")) { ]]

<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Sprout</title>
	<subtitle>Sprout</subtitle>
	<link href="http://[[ User.host ]]/feed" rel="self" />
	<link href="http://[[ User.host ]]" />
	<id>urn:uuid:1234</id>
	<updated></updated>
	<author>
		<name></name>
		<email></email>
	</author>

[[			while(it.hasNext()) {
				Article article = (Article) it.next(); ]]

	<entry>
		<title>[[ clean(article.meta(ARTICLE_TITLE).getString()) ]]</title>
		<link href="http://[[ User.host ]]/article?id=[[ article.getId() ]]" />
		<id>urn:uuid:[[ article.getId() ]]</id>
		<updated>[[ clean(article.time()) ]]</updated>
		<summary>[[ print(event, User.host, article, out); ]]</summary>
	</entry>

[[ 			} ]]

</feed>

[[		} else if(event.bit("rss")) { ]]

<rss version="2.0">
  <channel>
    <title>Sprout</title>
    <link>http://[[ User.host ]]</link>
    <description>Sprout</description>
    
[[			while(it.hasNext()) {
				Article article = (Article) it.next(); ]]
		
    <item>
      <title>[[ clean(article.meta(ARTICLE_TITLE).getString()) ]]</title>
      <link>http://[[ User.host ]]/article?id=[[ article.getId() ]]</link>
      <description>[[ print(event, User.host, article, out); ]]</description>
      <pubDate>[[ clean(article.time()) ]]</pubDate>
    </item>

[[ 			} ]]

  </channel>
</rss>

[[		} else { ]]

<data>
  <host>[[ User.host ]]</host>

[[			while(it.hasNext()) {
				Article article = (Article) it.next();
				out.print(article.toXML(1));
 			} ]]

</data>

[[		}
	}

	static void print(Event event, String host, Article article, Output out) throws Exception {
		Iterator it = article.child(FILE).iterator();
		while(it.hasNext()) {
			Node file = (Node) it.next();
			Data name = file.meta(FILE_NAME);
			Data type = file.meta(FILE_TYPE);
            
            if(type != null && type.equals("IMAGE")) {
				out.println(clean("<img src=\"http://" + host + "/" + File.path(event, file, name, "-200.jpeg") + "\" align=\"right\"/>"));
				break;
			}
		}

		LinkedList columns = article.columnize();
		it = columns.iterator();
		if(it.hasNext()) {
			String column = (String) it.next();
            out.print(clean(column));
			if(columns.size() > 1) {
				out.println("&#8230;");
			}
			else {
				out.println("");
			}
		}
	}
} ]]

